/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vm.project;

import de.micromata.opengis.kml.v_2_2_0.Document;
import de.micromata.opengis.kml.v_2_2_0.Folder;
import de.micromata.opengis.kml.v_2_2_0.GroundOverlay;
import de.micromata.opengis.kml.v_2_2_0.Kml;
import de.micromata.opengis.kml.v_2_2_0.KmlFactory;
import de.micromata.opengis.kml.v_2_2_0.LinearRing;
import de.micromata.opengis.kml.v_2_2_0.RefreshMode;
import de.micromata.opengis.kml.v_2_2_0.Style;
import de.micromata.opengis.kml.v_2_2_0.TimeSpan;
import java.io.File;
import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import jxl.write.DateTime;

/**
 *
 * @author florianfourure
 */
public class GestionKML2 {

    private ArrayList<Folder> folder=new ArrayList<>();
    private Kml kml;
    public GestionKML2(String nom){
        kml = KmlFactory.createKml();
    }
    
    public GestionKML2(Kml kml){
        this.kml = kml;
    }

    public void newFolder(String nom){
        if(folder.size()==0){
            folder.add(kml.createAndSetFolder().withName(nom).withOpen(true));  
        }else{
            folder.add(getFolder().createAndAddFolder().withName(nom).withOpen(true)); 
        }
    }
    
    public void closeFolder(){
        folder.remove(folder.size()-1);
    }
    
    public Folder getFolder() {
        return folder.get(folder.size()-1);
    }

    
    /**/
    
    public void addTimeStand(Date debut,Date fin){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm");
        TimeSpan ts = new TimeSpan().withBegin(sdf.format(debut));
        ts.withEnd(sdf.format(fin));
        getFolder().setTimePrimitive(ts);
       // getFolder().setTimePrimitive(ts);      
    }
    
  
    public void groundOverlay(String nom,String desc,String image,double dnorth,double dsouth,double deast,double dwest) throws FileNotFoundException{
        final GroundOverlay groundoverlay = getFolder().createAndAddGroundOverlay()
        .withName(nom)
        .withColor("9fffffff")
        .withDrawOrder(1);

        groundoverlay.createAndSetIcon()
        .withHref(image)
        .withRefreshMode(RefreshMode.ON_INTERVAL)
        .withRefreshInterval(86400d)
        .withViewBoundScale(0.75d);

        groundoverlay.createAndSetLatLonBox()
        .withNorth(dnorth)
        .withSouth(dsouth)
        .withEast(deast)
        .withWest(dwest)
        .withRotation(180);
    }
    
    public void isoValeur(String nom,ArrayList<Vecteur> tabVec) throws FileNotFoundException{
        this.newFolder(nom);
            for(Vecteur v : tabVec){
                placemark(nom, v.getP1(), v.getP2());
            }
        this.closeFolder();
    }
    
    public void placemark(String nom,Point p1, Point p2) throws FileNotFoundException {
        placemark(nom, p1,p2,"ff000000");
    }
    
    public void placemark(String nom, Point p1, Point p2,String couleur) throws FileNotFoundException{
        final Document document = getFolder().createAndAddDocument();
        final Style style = document.createAndAddStyle().withId("linestyleExample");

        style.createAndSetLineStyle()
                .withColor(couleur)
                .withWidth(8.0d);
        LinearRing lr = document.createAndAddPlacemark().withName(nom).withStyleUrl("#linestyleExample")
        .createAndSetPolygon().createAndSetOuterBoundaryIs().createAndSetLinearRing();
       lr.addToCoordinates(p1.getX(), p1.getY());
       lr.addToCoordinates(p2.getX(), p2.getY());
    }
    
    public void afficherTouteLesSations (Collection<StationsInfos> sations,String nom) throws FileNotFoundException {
        final Folder folder2 = getFolder().createAndAddFolder().withName(nom).withOpen(false);       
        for(StationsInfos si : sations){
            folder2.createAndAddPlacemark().withName(si.getNom()).createAndSetPoint().addToCoordinates(si.getX()+","+si.getY());
        }
    }
    
    public void afficherTouteLesPoints (Collection<Point> sations,String nom) throws FileNotFoundException {
        final Folder folder2 = getFolder().createAndAddFolder().withName(nom).withOpen(false);       
        for(Point si : sations){
            folder2.createAndAddPlacemark().withName(Double.toString(si.getV())).createAndSetPoint().addToCoordinates(si.getX()+","+si.getY());
        }
    }
    
    public void marshal(String nom) throws FileNotFoundException{
        kml.marshal(new File(nom+".kml"));
    }
}
   
